package com.tan.service.impl;

import com.tan.dao.UserDao;
import com.tan.domain.User;
import com.tan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author by 团子
 * @Classname UserServiceImpl
 * @Description TODO
 * @Date 2022/1/7 10:20
 * @Created by 团子
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<User> findAll() throws Exception {

        String key = "findAll";
        List<User> users = (List<User>) redisTemplate.boundValueOps(key).get();
        if(users != null && users.size() != 0){
            return users;
        }

        List<User> list = userDao.findAll();
        System.out.println("list:" + list);
        if(list != null && list.size() != 0){
            redisTemplate.boundValueOps(key).set(list,300, TimeUnit.SECONDS);
        }
        return list;
    }

    @Override
    public String save(User user) {
        return userDao.save(user).getUname();
    }
}
